package top.songxs.yygh.hosp.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.songxs.yygh.common.result.R;
import top.songxs.yygh.common.utils.JwtHelper;
import top.songxs.yygh.hosp.user.service.PatientService;
import top.songxs.yygh.hosp.user.util.AuthContextHolder;
import top.songxs.yygh.model.user.Patient;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags = "就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
//就诊人管理
public class PatientController {

    @Autowired
    PatientService patientService;

    //获取用户的就诊人列表
    @GetMapping("auth/findAll")
    public R list(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        List<Patient> list = patientService.findAllUserId(userId);
        return R.ok().data("list", list);

    }

    //根据id获取就诊人信息
    @GetMapping("auth/{id}")
    public R getPatient(@PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return R.ok().data("patient", patient);
    }

    //添加就诊人
    @PostMapping("auth/save")
    public R savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    //修改就诊人
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return R.ok();
    }

    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public R removePatientById(@PathVariable("id") Long id) {
        patientService.removeById(id);
        return R.ok();
    }

    @ApiOperation("获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id) {

        return patientService.getById(id);
    }
}
